﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using rock.Model;
using rock.DAL;
using rock.UI.common;
using rock.BLL;

namespace rock.UI.houseinfo
{
    public partial class FormHouseOwnerUpdate : Form
    {
        public FormHouseOwnerUpdate()
        {
            InitializeComponent();
            
            this.AcceptButton = this.button1;
            this.CancelButton = this.button2;
            this.MaximizeBox = false;
            this.MinimizeBox = false;
            this.WindowState = FormWindowState.Normal;
            this.StartPosition = FormStartPosition.CenterScreen;
            this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedSingle;
            this.Icon = global::rock.Properties.Resources.house;
        }

        private ModelHOUSE_OWNER modelHOUSE_OWNER = new ModelHOUSE_OWNER();
        public ModelHOUSE_OWNER _ModelHOUSE_OWNER
        {
            set { modelHOUSE_OWNER = value; }
            get { return modelHOUSE_OWNER; }
        }
        DalHOUSE_OWNER dalHOUSE_OWNER = new DalHOUSE_OWNER();


        private void FormHouseOwnerUpdate_Load(object sender, EventArgs e)
        {
            
            
            this.textBox1name.Text = this._ModelHOUSE_OWNER.NAME;
            this.textBox2phone.Text = this._ModelHOUSE_OWNER.PHONE;
            this.textBox3address.Text = this._ModelHOUSE_OWNER.ADDRESS;
            this.textBox4note.Text = this._ModelHOUSE_OWNER.NOTE;
        }

        private Boolean check()
        {
            try
            {
                StringUtils.checkText(this.textBox1name.Text, 2, 30, "业主名称");



                StringUtils.checkText(this.textBox2phone.Text, 0, 300, "联系电话");
                StringUtils.checkText(this.textBox3address.Text, 0, 50, "地址信息");
                StringUtils.checkText(this.textBox4note.Text, 0, 300, "备注信息");
      

                //更新.原值不检测
                if (dalHOUSE_OWNER.Exists(this._ModelHOUSE_OWNER.SEQID))
                {
                    if (_ModelHOUSE_OWNER.NAME.Equals(this.textBox1name.Text.Trim()))
                    {
                        return true;
                    }
                }
                String existSql = "select count(1) from HOUSE_OWNER where NAME = '{0}'";
                existSql = String.Format(existSql, this.textBox1name.Text);
                if (BllCommonUtil.Exists(existSql))
                {
                    MessageBox.Show("此业主名称值已存在在,请输入其它的值", "提示");
                    return false;
                }


            }
            catch (Exception e)
            {
                MessageBox.Show(e.Message, "提示");
                return false;
            }
            return true;
        }


        private void button1_Click(object sender, EventArgs e)
        {
            if (!check())
            {
                return;
            }

            this._ModelHOUSE_OWNER.NAME = this.textBox1name.Text;
            this._ModelHOUSE_OWNER.PHONE = this.textBox2phone.Text;
            this._ModelHOUSE_OWNER.ADDRESS = this.textBox3address.Text;
            this._ModelHOUSE_OWNER.NOTE = this.textBox4note.Text;
            if (dalHOUSE_OWNER.Exists(this._ModelHOUSE_OWNER.SEQID))
            {
                dalHOUSE_OWNER.Update(this._ModelHOUSE_OWNER);
            }
            else
            {
                dalHOUSE_OWNER.Add(this._ModelHOUSE_OWNER);
            }
            //触发代理
            oneRefreshParentData();
            this.Close();
        }




        //取消
        private void button2_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        //代理
        public delegate void RefreshParentData();
        public RefreshParentData oneRefreshParentData;

       
    }
}
